<?php

/**
 * Classe prenant en charge les requetes SQL de type [INSERT]
 *
 * @category   McCube
 * @package    Db
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 * @see        http://www.php.net/manual/book.pdo.php
 */
class McCube_Db_Query_Insert extends McCube_Db_Query{

    protected $_onDuplicateKey = '';

    public function build(){
        $q  = 'INSERT INTO '.$this->_table
            .' ('.implode(' , ', $this->_fields).')'
            .' VALUES '.implode(' , ', $this->_values)
        ;

        if(!empty($this->_onDuplicateKey))
           $q .= ' ON DUPLICATE KEY UPDATE '.$this->_onDuplicateKey;

        return $q.';';
    }

    public function set($set){
        if(!$this->_fields)
            $this->_fields = array_keys($set);

        foreach($set as &$value)
            $value = $this->_database->quote($value);

        $this->_values[] = '('.implode(' , ', $set).')';
        return $this;
    }

    public function sets($sets){
        if(!$this->_fields)
            $this->_fields = array_keys($sets[0]);

        foreach($sets as $set)
            $this->_values[] = '('.implode(' , ', $set).')';

        return $this;
    }
}